package com.estar.assistsurvy.activity;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.SubjectListviewAdapter;
import com.estar.assistsurvy.entity.EsQuestionsList;
import com.estar.assistsurvy.entity.OptionList;
import com.estar.assistsurvy.entity.QuestionList;
import com.estar.assistsurvy.entity.RequestAnswerSubmission;
import com.estar.assistsurvy.entity.RequestOnlineQueryQuestions;
import com.estar.assistsurvy.entity.ResponseMsg;
import com.estar.assistsurvy.entity.ResponseQueryQuestions;
import com.estar.assistsurvy.thread.AnswerSubmissionThread;
import com.estar.assistsurvy.thread.QueryQuestionsThread;
import com.estar.assistsurvy.utils.MightypleUtil;
import com.estar.assistsurvy.widget.NotScrollListview;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;

public class ExamOnlineActivity extends BaseActivity implements OnClickListener,OnItemClickListener{

	private NotScrollListview subjectList;
	private TextView returnHome,subject_index,subject_content;
	private Button submit;
	//	private ArrayList<Subject> subjects;
	//	private String[] contents=new String[]{"驾驶人有下列哪种违法行为一次记6分？","下列哪种违法行为的机动车驾驶人将被一次记12分？"};
	//	private String[] options0=new String[]{"使用其他车辆行驶证","饮酒后驾驶机动车","车速超过规定时速50%以上","违法占用应急车道行驶"};
	//	private String[] options1=new String[]{"驾驶故意污损号牌的机动车上道路行驶","机动车驾驶证被暂扣期间驾驶机动车的","以隐瞒、欺骗手段补领机动车驾驶证的 ","驾驶机动车不按照规定避让校车的"};
	private int index=0;
	private Intent intent;
	private ResponseQueryQuestions queryQuestions;
	private EsQuestionsList[] esQuestionsList;
	private OptionList[] optionList;
	private SubjectListviewAdapter adapter;
	private String uuid,deviceId;
	private int isRight=0;
	private SharedPreferences preference;
	private ProgressDialog dialog;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(dialog!=null){
				dialog.dismiss();
			}
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;

			case 0:

				queryQuestions=new ResponseQueryQuestions();
				queryQuestions=(ResponseQueryQuestions) msg.obj;
				showShortToast(queryQuestions.getMessages());
				esQuestionsList=queryQuestions.getEsQuestionsList();
				setView();
				break;

			case 1:
				setView();
				break;

			case 2:
				dialog.dismiss();
				ResponseMsg message=new ResponseMsg();
				message=(ResponseMsg) msg.obj;
				showShortToast(message.getMessages());
				intent=new Intent(ExamOnlineActivity.this, CetificationActivity.class);
				startActivity(intent);
				finish();
				overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
				break;

			default:
				break;
			}
		};
	};

	private RequestAnswerSubmission answerSubmission;
	private QuestionList[] questionList=new QuestionList[20];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_examonline);
		initView();
	}

	private void initView(){
		subjectList=find(R.id.subjectList);
		subjectList.setOnItemClickListener(this);
		returnHome=find(R.id.returnBack);
		returnHome.setOnClickListener(this);
		subject_content=find(R.id.subject_content);
		subject_index=find(R.id.subject_index);
		submit=find(R.id.submit);
		submit.setVisibility(View.GONE);
		submit.setOnClickListener(this);

		preference=getSharedPreferences("user", MODE_PRIVATE);
		uuid=preference.getString("uuid", "");
		deviceId=MightypleUtil.getDeviceId(ExamOnlineActivity.this);
		dialog=ProgressDialog.show(this, null, "正在获取题目…", false, true);
		RequestOnlineQueryQuestions queryQuestions=new RequestOnlineQueryQuestions(deviceId, uuid);
		Log.i("wl", queryQuestions.toString());
		new QueryQuestionsThread(ExamOnlineActivity.this, queryQuestions, handler).start();

		answerSubmission=new RequestAnswerSubmission();
		answerSubmission.setUUID(uuid);
		answerSubmission.setDeviceUUID(deviceId);
	}

	public void setView(){
		EsQuestionsList esQuestions=esQuestionsList[index];
		subject_index.setText(index+1+"/"+esQuestionsList.length);
		subject_content.setText(esQuestions.getContent());
		optionList=esQuestions.getOptionList();
		adapter=new SubjectListviewAdapter(optionList, ExamOnlineActivity.this);
		subjectList.setAdapter(adapter);
		if(index==esQuestionsList.length-1){
			submit.setVisibility(View.VISIBLE);
		}else{
			submit.setVisibility(View.GONE);
		}
	}

	//	private void initView(){
	//
	//		setSubjects();
	//		intent=getIntent();
	//		if(intent.getExtras()!=null){
	//			index=intent.getExtras().getInt("index");
	//		}
	//		subjectList=find(R.id.subjectList);
	//		subjectList.setOnItemClickListener(this);
	//		returnHome=find(R.id.returnBack);
	//		returnHome.setOnClickListener(this);
	//		subject_content=find(R.id.subject_content);
	//		subject_content.setText(subjects.get(index).getContent());
	//		adapter=new SubjectListviewAdapter(subjects.get(index), ExamOnlineActivity.this);
	//		subjectList.setAdapter(adapter);
	//		subject_index=find(R.id.subject_index);
	//		subject_index.setText(index+1+"/"+subjects.size());
	//
	//		preference=getSharedPreferences("user", MODE_PRIVATE);
	//		uuid=preference.getString("uuid", "");
	//		deviceId=MightypleUtil.getDeviceId(ExamOnlineActivity.this);
	//		RequestOnlineQueryQuestions queryQuestions=new RequestOnlineQueryQuestions(deviceId, uuid);
	//		new QueryQuestionsThread(ExamOnlineActivity.this, queryQuestions, handler).start();
	//	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			intent=new Intent(ExamOnlineActivity.this, CetificationActivity.class);
			startActivity(intent);
			finish();
			overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);

			break;

		case R.id.submit:
			dialog=ProgressDialog.show(this, null, "答题提交…", false, true);
			answerSubmission.setQuestionList(questionList);
			answerSubmission.setAnswerScore(isRight*100/esQuestionsList.length+"");
			Log.i("wl", "提交"+answerSubmission.toString());
			new AnswerSubmissionThread(ExamOnlineActivity.this, handler, answerSubmission).start();
			break;

		default:
			break;
		}
	}

	//	private void setSubjects(){
	//		subjects=new ArrayList<Subject>();
	//		Subject sub0=new Subject(contents[0], options0);
	//		subjects.add(sub0);
	//		Subject sub1=new Subject(contents[1], options1);
	//		subjects.add(sub1);
	//	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		adapter.setSelectItem(position);
		adapter.notifyDataSetChanged();

		//提交答案
		String optionUUID, sign, questionUUID;
		optionUUID=esQuestionsList[index].getOptionList()[position].getId();
		questionUUID=esQuestionsList[index].getOptionList()[position].getQuestionsId();
		sign=esQuestionsList[index].getOptionList()[position].getSign();
		if(sign.equalsIgnoreCase(esQuestionsList[index].getAnswer())){
			isRight++;
		}
		QuestionList question=new QuestionList(optionUUID, sign, questionUUID);
		questionList[index]=question;
		index++;
		if(index<=esQuestionsList.length-1){
			handler.sendEmptyMessageDelayed(1, 500);
			//			setView();

		}

	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
//		super.onBackPressed();
		intent=new Intent(ExamOnlineActivity.this, CetificationActivity.class);
		startActivity(intent);
		finish();
		overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);

	}
}
